//
//  SettingViewController.m
//  Project_B
//
//  Created by 甘霞 on 15/11/27.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingTableViewCell.h"
#import "CalculateFileSize.h"
#import "WXMoreEmailViewController.h"

@interface SettingViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) CGFloat cacheSize;
@property (nonatomic, copy) NSString *str;

@property (nonatomic, strong) UIButton *closeBtn;

@property (nonatomic, strong) NSMutableArray *arr;

@property (nonatomic, strong) UIView *backView;
@end

@implementation SettingViewController

- (void)createTableView  {

    [self createTableViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView registerClass:[SettingTableViewCell class] forCellReuseIdentifier:@"setting"];
    
    self.closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.closeBtn setImage:[UIImage imageNamed:@"afternoon_tea_preview_close"] forState:UIControlStateNormal];
    [self.closeBtn addTarget:self action:@selector(closeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    [self.closeBtn setFrame:CGRectMake(self.view.frame.size.width / 2 - 80 , 0, 30, 40)];
    [self.backView setBackgroundColor:[UIColor clearColor]];
    [self.backView addSubview:self.closeBtn];
    self.tableView.tableFooterView = self.backView;
}

- (void)closeBtnAction {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CloseAction" object:nil];
    
    [UIView animateWithDuration:1.5f
                     animations:^{
                         
                         [self.view setFrame:CGRectMake(60, kScreenHeight, kScreenWidth - 2 * 60, kScreenHeight - 400)];
                     }];
}


- (void)show {
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    
    [self.view setFrame:CGRectMake(60, kScreenHeight, kScreenWidth - 2 * 60, kScreenHeight - 400)];
    
    [self.view setBackgroundColor:[UIColor clearColor]];
    
    [window addSubview:self.view];
    
    [UIView animateWithDuration:1.5f
                     animations:^{
                         
        [self.view setFrame:CGRectMake(60, 230, kScreenWidth - 2 * 60, kScreenHeight - 400)];
    }];
}

- (void)hidden {
    
    [UIView animateWithDuration:1.5f
                     animations:^{
                         
                         [self.view setFrame:CGRectMake(60, kScreenHeight, kScreenWidth - 2 * 60, kScreenHeight - 400)];
                     }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createTableView];
    
    NSDictionary *user = @{
                           @"image" : @"btn-recent-afternoontea-list",
                           @"text"  : @"用户",
                           @"switch": @YES};
    
    NSDictionary *warning = @{
                              @"image" : @"menu_icon_notification",
                              @"text"  : @"每日提醒",
                              @"switch": @NO};
    
    NSDictionary *clearCache = @{
                              @"image" : @"icon-user-broom",
                              @"text"  : @"清除缓存",
                              @"switch": @YES};
    
    NSDictionary *feekback = @{
                              @"image" : @"icon-user-review",
                              @"text"  : @"和我们聊聊",
                              @"switch": @YES};
    
    
    NSDictionary *login = @{
                            @"image" : @"icon-user-logout",
                            @"text"  : @"退出登录",
                            @"switch": @YES};
    

    self.arr = [@[user, warning, clearCache, feekback, login] mutableCopy];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeView) name:@"recover" object:nil];
}

- (void)changeView
{
    [self.view setFrame:CGRectMake(60, 230, kScreenWidth - 2 * 60, kScreenHeight - 400)];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"setting"];
    cell.backgroundColor = [UIColor blackColor];
    cell.dic = self.arr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    if (indexPath.row == 2) {
        [self clearCache];
    }
    
    if (indexPath.row == 3) {
        WXMoreEmailViewController *emailVC = [[WXMoreEmailViewController alloc] init];
        [self presentViewController:emailVC animated:YES completion:nil];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    [self hidden];
    [self.view setFrame:CGRectMake(60, 230, kScreenWidth - 2 * 60, kScreenHeight - 400)];
}

#pragma mark - 清除缓存
- (void)clearCache {
    NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = cache[0];
    CalculateFileSize *calculateFileSize = [CalculateFileSize defaultCalculateFileSize];
    float cacheSize = [calculateFileSize folderSizeAtPath:cachePath];
    //NSLog(@"sandBoxPath %@",cachePath);
    
    //NSLog(@"缓存大小是:%fM",cacheSize);
    self.cacheSize = cacheSize;
    //NSLog(@"self.cacheSize%f",self.cacheSize);
    
    self.str = [NSString stringWithFormat:@"目前缓存大小是%.2fM，是否删除?",self.cacheSize];
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:self.str
                                                                preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *cachePath = cache[0];
        NSFileManager *manager = [NSFileManager defaultManager];
        [manager removeItemAtPath:cachePath error:nil];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertView addAction:sureAction];
    [alertView addAction:cancelAction];
    [self presentViewController:alertView animated:YES completion:nil];
}

@end
